clear all

// SET MACROS
global Input  "Y:/limited/Michigan_CTE/funding_change/data_final"
global Output "Y:/limited/Michigan_CTE/funding_change/output/figures"

qui include "Y:/limited/Michigan_CTE/funding_change/code/analyze/figures/00_colors.do"

local demographics "female black hisp another_race sped lep econdis"
local academics "test_avg_g8 test_avg_sq_g8 test_avg_g8_imp attendance_g8 attendance_g8_imp"

// LOAD DATA
use $Input/compiled_student_analysis_data.dta

keep if grad_year >= 2012

replace c2014 = 0

gen cohort = .

gen povQ0_over_time     = .
gen povQ0_over_time_l95 = .
gen povQ0_over_time_u95 = .
gen povQ1_over_time     = .
gen povQ1_over_time_l95 = .
gen povQ1_over_time_u95 = .

forvalues y = 0/1 {
  eststo: reghdfe cte_comp c2012-c2019 `demographics' `academics' if district_most_poor == `y' , a(district) cluster(district)
  
  local i = 1
  forvalues x = 2012/2019 {
    replace povQ`y'_over_time = 100 * _b[c`x'] in `i'
    replace povQ`y'_over_time_l95 = 100 *  _b[c`x'] - (invttail(e(df_r), 0.025) * 100 * _se[c`x']) in `i'
    replace povQ`y'_over_time_u95 = 100 *  _b[c`x'] + (invttail(e(df_r), 0.025) * 100 * _se[c`x']) in `i'
    replace cohort = `x' if missing(cohort) in `i'
    local ++i
  }
}

keep cohort povQ0* povQ1*
drop if missing(cohort)

// DRAW GRAPH
tw (rarea povQ0_over_time_l95 povQ0_over_time_u95 cohort, color("$c2%30") lwidth(none)) ///
   (rarea povQ1_over_time_l95 povQ1_over_time_u95 cohort, color("$c1%30") lwidth(none)) ///
   (connected povQ0_over_time povQ1_over_time cohort, sort ///
   mc("$c2" "$c1") ///
   m(T O) ///
   lc("$c2" "$c1") ///
   lp(dash solid)), ///
   scale(1.1) ///
   xline(2014.5 2015.5, lc(gs12) lp(-)) ///
   yline(0, lc(gs11)) ///
   text(17.9 2014.5 "Funding" "increased," "new formula" "announced", placement(0) color(black) box bcolor(white) margin(b=1)) ///
   text(19.1 2015.5 "New" "formula" "applied", placement(0) color(black) box bcolor(white) margin(t=2 b=1)) ///
   xlabel(2012/2019) ///
   xtitle(" " "Cohort Graduation Year", margin(t=2)) ///
   ylabel(-5(5)20, angle(0) glc(gs13) glw(vthin) glp(shortdash) gmax) ///
   ytitle("Percentage Point Change" " ") ///
   legend(order(4 3) label(4 "Poorest 25% of Districts") label(3 "Wealthiest 75% of Districts") rows(1) symxsize(7) region(lstyle(none))) ///
   graphregion(color(white)) bgcolor(white) ///
   xsize(7) ysize(3.25)  ///
	 scheme(s2color)


// EXPORT GRAPH
graph save $Output/05b_reg_district_wealth.gph, replace
*graph export $Output/reg_district_wealth.png, replace
